package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.onImageClickListener;

import java.util.List;

/**
 * Created by Caleb.F
 * on 2020-04-15
 * at 20:23
 */
public class MyTrafficMinusAdapter extends RecyclerView.Adapter<MyTrafficMinusAdapter.MyRecyclerViewHolder> {
    private List<String> mlist;
    private Context mContext;
    private onLongClickListener mOnLongClickListener;
    private onImageClickListener mOnImageClickListener;
    private boolean showIcon;

    public MyTrafficMinusAdapter(Context context, List<String> list) {
        mContext = context;
        mlist = list;
    }

    @NonNull
    @Override
    public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_trafficinfo_item, parent, false);
        return new MyRecyclerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position) {
        holder.mTvRecyclerItem.setText(mlist.get(position));
        if(mOnLongClickListener!=null){
            holder.mTvRecyclerItem.setOnClickListener(v->{
                mOnLongClickListener.longClick();
            });
        }
        if(showIcon){
            holder.mIvImage.setImageResource(R.mipmap.minus);
            holder.mIvImage.setEnabled(true);
            holder.mIvImage.setOnClickListener(v->{
                mOnImageClickListener.imageClick(holder.getAdapterPosition());
            });
        }
        else{
            holder.mIvImage.setEnabled(false);
        }

    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    class MyRecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView mTvRecyclerItem;
        ImageView mIvImage;

        public MyRecyclerViewHolder(View convertView) {
            super(convertView);
            mTvRecyclerItem = convertView.findViewById(R.id.tv_recycler_item);
            mIvImage = convertView.findViewById(R.id.iv_image);
        }
    }

    public interface onLongClickListener {
        void longClick();
    }

    public void setLongClickListener(onLongClickListener onLongClickListener) {
        mOnLongClickListener = onLongClickListener;
    }


    public void setonImageClick(onImageClickListener onImageClickListener) {
        mOnImageClickListener = onImageClickListener;
    }

    public void setShowIcon(boolean showIcon) {
        this.showIcon = showIcon;
        notifyDataSetChanged();
    }


}
